function init() {
	$(".img-ul li").bind("click",function(){

      	 $(".img-ul li .body").removeClass("body-active");
      	 $(".img-ul li .header").removeClass("header-active");
      	 $(this).find(".header").addClass("header-active");

      	 $(this).find(".body").addClass("body-active");

      });
      $(".apply-cago .close, .apply-cago button.cancel").bind("click",function(){
      	$(".apply-cago").css("display","none");
      	$(".background-panel").css("display","none");
      });
      // $("#query-btn").bind("click",function(){
      // 	$(".apply-cago").css("display","block");
      // 	$(".background-panel").css("display","block");
      // })
      
     
}

define(
	[
	appBasePath + '/scripts/module.js',
	],
	function (substanceModule) {

    'use strict';

    substanceModule.controller('Substance.GoodsShowController', [
        '$scope',
        '$state',
        '$stateParams',
        'gbits.websns.substance.config',
        'Substance.GoodsService',
        'Substance.GoodsApplyService',
        '$timeout',
        function($scope, $state, $stateParams, config, goodsService, goodsApplyService, $timeout) {
        	$scope.list = [];        	
        	$scope.queryData = {
        			name: '',
        			startPrice: '',
        			endPrice: ''
        	};
        	$scope.applyData = {
        			applyNum: 1,
        			applyText: ''
        	};
        	$scope.page = {
        			currentPage: 1
        	};
        	$scope.storeInCount = '';
        	// 查询
        	$scope.query = function() {
        		
        		/*
        		if (!$scope.queryData.name && !$scope.queryData.startPrice && !$scope.queryData.endPrice) {
        			$scope.errorMsg({message: "查询条件不能为空"});
        			return false;
        		}
        		*/
        		
        		$timeout(function(){
        			$scope.page.currentPage = 1;
            		$scope.loadData();
        		});
        	}
        	
        	$scope.addNum = function(v) {
        		$timeout(function(){
        			var n = parseInt($scope.applyData.applyNum), l = n+v;
        			var max = $("#number").attr('max');
        			if (l < 0) {
        				l = 1;
        			}
        			if (l > max) {
        				l = max;
        			}
        			if (l > 0 && l <= max) {
        				$scope.applyData.applyNum = l;
        			}
        		});
        	}
        	
        	$scope.clean = function() {
        		$timeout(function(){
        			$scope.queryData = {
                			name: '',
                			startPrice: '',
                			endPrice: ''
                	};
        			$scope.page.currentPage = 1;
        			$scope.loadData();
        		});
        	}
        	
        	$scope.showImage = function(image) {
        		$timeout(function(){
        			$scope.selectImage = image;
        		});
        	}
        	
        	$scope.showGoods = function(g) {
        		$timeout(function(){
        			if (!g.storeCount || g.storeCount == 0) {
        				$scope.errorMsg({code: '-1', message: "库存不足"});
        				return false;
        			}
        			
        			$scope.currentGoods = g;
        			$scope.selectImage = g.goodImages[0];
        			$scope.applyData = {
        					applyNum: 1,
                			applyText: ''
                	};
        			$(".apply-cago").show();
                    $(".background-panel").show();
        		});
        	}
        	
        	var ld = function loadData() {
        		$scope.list = [];
        		goodsService.showlist($scope.page.currentPage, $scope.queryData).then(function(data) {
        			$timeout(function(){
        				$scope.list = data.data;
            			$scope.page = data;
            			$scope.page.prePage = 1;
            			$scope.page.nextPage = data.lastPage;
            			if (data.currentPage > 1) {
            				$scope.page.prePage = data.currentPage - 1;
            			}
            			if (data.currentPage < $scope.page.lastPage - 1) {
            				$scope.page.nextPage = data.currentPage + 1;
            			}
            			$scope.page.showPages = [];
            			for (var i = data.currentPage - 5; i<=data.lastPage; i++) {
            				if (i>0) {
            					$scope.page.showPages.push(i);
            				}
            				if ($scope.page.showPages.length > 10) {
            					break;
            				}
            			}
        			})
        			
        		});
        	}
        	
        	$scope.applySubmit = function() {
        		
        		goodsApplyService.applyGoods($scope.currentGoods.id, $scope.applyData.applyNum, $scope.applyData).then(function(data) {
        			$timeout(function(){
        				if (data.success) {
        					$(".apply-cago").hide();
                            $(".background-panel").hide();
                            var gid = data.data.id;
                            for (var i=0; i<$scope.list.length; i++) {
        						var g = $scope.list[i];
        						if (g.id == gid) {
        							$scope.list[i] = data.data;
        							break;
        						}
        					}
            			} else {
            				$scope.errorMsg(data);
            			}
        			});
        		});
        	}
        	
        	init();
        	
        	$scope.loadData = ld;
        	$scope.loadData();
        }
    ]);
});